﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.SystemService;
using EHR.Common;
using System.Windows.Media.Imaging;
using System.IO;
using CommunitySystem.PageView;

namespace CommunitySystem.Views.大图标
{
    public partial class TBControl : AsyncCtrl, IReturnListenerc
    {
        public TBControl()
        {
            InitializeComponent();
            init();
        }

        /// <summary>
        /// 获取所有的主菜单
        /// </summary>
        private void init()
        {
            DataService.DataServiceClient client = new DataService.DataServiceClient();
            UserInfo u = Session.LoginUser;
            client.GetSubMenuByUserIDCompleted += new EventHandler<DataService.GetSubMenuByUserIDCompletedEventArgs>(client_GetSubMenuByUserIDCompleted);
            client.GetSubMenuByUserIDByRoleCompleted += new EventHandler<DataService.GetSubMenuByUserIDByRoleCompletedEventArgs>(client_GetSubMenuByUserIDByRoleCompleted);
          
            if (Session.LoginUser.RoleID == 2)//人事干部
              client.GetSubMenuByUserIDByRoleAsync(u.UserID, 0,u.RoleID.Value);
          else if (Session.LoginUser.RoleID == 1) //管理员
              client.GetSubMenuByUserIDAsync(u.UserID, 0);
           
        }

        void client_GetSubMenuByUserIDByRoleCompleted(object sender, DataService.GetSubMenuByUserIDByRoleCompletedEventArgs e)
        {
            var items = e.Result.Where(p => p.PID == 0).ToList();


            InitTB(items);
        }

        void client_GetSubMenuByUserIDCompleted(object sender, DataService.GetSubMenuByUserIDCompletedEventArgs e)
        {
            var items = e.Result.Where(p => p.PID == 0).ToList();


            InitTB(items);
            
        }

        private void InitTB(List<DataService.MenuItem> items)
        {
            foreach (var i in items)
            {
                Border bo = new Border() { Cursor = Cursors.Hand, BorderThickness = new Thickness(1, 1, 1, 1), Width = 150, Height = 150, Margin = new Thickness(10, 10, 10, 10) };
                bo.Tag = i;
                bo.MouseLeftButtonDown += new MouseButtonEventHandler(bo_MouseLeftButtonDown);
                StackPanel sp = new StackPanel() { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Center };
                StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };

                if (i.icon != null && i.icon.Length > 0)
                {
                    Image img = new Image() { Width = 100, Height = 100, Opacity = 1, RenderTransformOrigin = new Point() { X = 0.5, Y = 0.5 } };
                    img.Tag = i;
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.icon, 0, i.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    TextBlock tb = new TextBlock() { Margin = new Thickness(0, 10, 0, 0), Text = i.Title.ToString(), Foreground = new SolidColorBrush(Colors.Black), FontSize = 14, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center };

                    Grid sp11 = new Grid() { Height = 40, Width = 40, Margin = new Thickness(-25, -10, 0, 0), VerticalAlignment = VerticalAlignment.Top };
                    ImageBrush img1 = new ImageBrush();
                    BitmapImage bi = new BitmapImage();
                    bi.UriSource = new Uri("/CommunitySystem;component/TIcons/IndexImg/20.png", UriKind.RelativeOrAbsolute);
                    img1.ImageSource = bi;
                    sp11.Background = img1;

                    TextBlock tb1 = new TextBlock() { Text = i.xiaoxicount.ToString(), Foreground = new SolidColorBrush(Colors.White), FontWeight = FontWeights.Bold, FontSize = 20, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
                    sp11.Children.Add(tb1);

                    sp1.Children.Add(img);

                    if (i.xiaoxicount != 0)
                        sp1.Children.Add(sp11);

                    sp.Children.Add(sp1);
                    sp.Children.Add(tb);
                    bo.Child = sp;
                    wp.Children.Add(bo);
                }
            }
        }
        /// <summary>
        /// 点击图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Border ii = sender as Border;
            DataService.MenuItem mode = ii.Tag as DataService.MenuItem;
            //PageIndex view = new PageIndex(mode);
            //Nav.ShowNextPage(mode.Title.ToString(), view, this);
            if (mode.xiaoxicount != 0)
            {
                SysInfoControl view = new SysInfoControl(mode);
                Nav.ShowNextPage(mode.Title.ToString() + "-消息查看", view, this);
            }
            else
            {
                PageIndex view = new PageIndex(mode);
                Nav.ShowNextPage(mode.Title.ToString(), view, this);
            }

        
           
        }



        public string Title
        {
            get { return ""; }
            set { Title = value; }
        }

        public bool PageLoaded
        {
            get;
            set;
        }

        public NavMoveSwitch Nav { get; set; }

        public void BeginReturn()
        {
        
        }
  
    }
}
